package com.swipemaster.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.google.analytics.tracking.android.EasyTracker;
import com.swipemaster.R;
import com.swipemaster.common.Const;
import com.swipemaster.common.Coords;
import com.swipemaster.common.CustomGameSettings;
import com.swipemaster.common.Global;
import com.swipemaster.game.modes.CustomMode;

/** Type of game where player can specify game options like bouncing, particles, number and size of enemies. */
public class CustomGameActivity extends CommonActivity implements SeekBar.OnSeekBarChangeListener
{
	public static final int MAX_CUSTOM_ENEMIES = 10;
	public static final int MIN_CUSTOM_ENEMIES = 2;
	public static final int MAX_ENEMY_SIZE = 10;

	public static final double MAX_RADIUS = 0.06;
	public static final double MIN_RADIUS = 0.02;

	private static final int ANIM_DURATION = 800;
	private static final int ANIM_OUT_DURATION = 600;

	private static final int maxRadiusPx = (int) Coords.mulByDiag(MAX_RADIUS);
	private static final int minRadiusPx = (int) Coords.mulByDiag(MIN_RADIUS);

	private TextView numberOfEnemiesText;
	private TextView numberOfEnemies;
	private SeekBar numberOfEnemiesBar;
	private TextView bouncingText;
	private ToggleButton bouncingToggle;
	private TextView particlesText;
	private ToggleButton particlesToggle;
	private TextView sizeOfEnemiesText;
	private ImageView enemySizeImage;
	private SeekBar enemySizeBar;
	private Button playCustomButton;
	private Button backCustomButton;

	private boolean backAlreadyPressed = false;
	private final Handler handler = new Handler();

	public static int getEnemiesPx(int sizeOnBar)
	{
		final double ratio = (double) sizeOnBar / MAX_ENEMY_SIZE;
		final int pxRange = maxRadiusPx - minRadiusPx;
		return minRadiusPx + (int) (pxRange * ratio);
	}

	@Override
	public void onBackPressed()
	{
		if (backAlreadyPressed)
		{
			return;
		}
		backAlreadyPressed = true;

		setOutAnimations();

		handler.postDelayed(new Runnable()
		{
			@Override
			public void run()
			{
				setViewsVisibility(View.INVISIBLE);
			}
		}, Const.BUTTON_CLICK_ANIM_DURATION + 200);

		handler.postDelayed(new Runnable()
		{
			@Override
			public void run()
			{
				CustomGameActivity.super.onBackPressed();
				overridePendingTransition(0, 0);
			}
		}, Const.BUTTON_CLICK_ANIM_DURATION + ANIM_OUT_DURATION);
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
	{
		if (seekBar.equals(numberOfEnemiesBar))
		{
			CustomGameSettings.numberOfEnemies = progress + MIN_CUSTOM_ENEMIES;
			numberOfEnemies.setText(CustomGameSettings.numberOfEnemies + "");
			numberOfEnemies.invalidate();
		}
		else if (seekBar.equals(enemySizeBar))
		{
			CustomGameSettings.enemiesSize = progress;
			setEnemyThumbnail(progress);
		}
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar)
	{
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar)
	{
	}

	public void onToggleClicked(View view)
	{
		// Perform action on clicks.
		final ToggleButton toggleButton = (ToggleButton) view;
		if (toggleButton.isChecked())
		{
			if (toggleButton.equals(bouncingToggle))
			{
				CustomGameSettings.bouncing = true;
			}
			else if (toggleButton.equals(particlesToggle))
			{
				CustomGameSettings.particles = true;
			}
		}
		else
		{
			if (toggleButton.equals(bouncingToggle))
			{
				CustomGameSettings.bouncing = false;
			}
			else if (toggleButton.equals(particlesToggle))
			{
				CustomGameSettings.particles = false;
			}
		}
	}

	void setEnemyThumbnail(int sizeOnBar)
	{
		final ViewGroup.LayoutParams params = enemySizeImage.getLayoutParams();
		final int radius = getEnemiesPx(sizeOnBar);
		params.width = params.height = 2 * radius;
		enemySizeImage.setLayoutParams(params);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		setContentView(R.layout.custom_game);
		EasyTracker.getInstance().activityStart(this);

		numberOfEnemiesText = (TextView) findViewById(R.id.number_of_enemies_text);
		numberOfEnemiesText.setTypeface(Global.app.consts.font);
		numberOfEnemies = (TextView) findViewById(R.id.number_of_enemies);
		numberOfEnemies.setTypeface(Global.app.consts.font);
		numberOfEnemiesBar = (SeekBar) findViewById(R.id.number_of_enemies_bar);
		numberOfEnemiesBar.setOnSeekBarChangeListener(this);

		bouncingText = (TextView) findViewById(R.id.bouncing_text);
		bouncingText.setTypeface(Global.app.consts.font);
		bouncingToggle = (ToggleButton) findViewById(R.id.bouncing_toggle);

		particlesText = (TextView) findViewById(R.id.particles_text);
		particlesText.setTypeface(Global.app.consts.font);
		particlesToggle = (ToggleButton) findViewById(R.id.particles_toggle);

		sizeOfEnemiesText = (TextView) findViewById(R.id.size_of_enemies_text);
		sizeOfEnemiesText.setTypeface(Global.app.consts.font);
		enemySizeImage = (ImageView) findViewById(R.id.enemy_size_image);
		enemySizeBar = (SeekBar) findViewById(R.id.enemy_size_bar);
		enemySizeBar.setOnSeekBarChangeListener(this);

		playCustomButton = (Button) findViewById(R.id.play_custom_button);
		playCustomButton.setTypeface(Global.app.consts.font);
		backCustomButton = (Button) findViewById(R.id.back_custom_button);
		backCustomButton.setTypeface(Global.app.consts.font);

		setSettingsValues();
		setAnimations();

		playCustomButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Global.app.gameMode = new CustomMode();
				final Intent intent = new Intent(getApplication(), GameActivity.class);
				startActivity(intent);
			}
		});

		backCustomButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				onBackPressed();
			}
		});

		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onStop()
	{
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	private void setAnimations()
	{
		final Animation animFromLeft = new TranslateAnimation(Animation.ABSOLUTE, -Global.app.consts.width,
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
		final Animation animFromRight = new TranslateAnimation(Animation.ABSOLUTE, 2 * Global.app.consts.width,
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);

		final Interpolator interpolator = new OvershootInterpolator(1.5f);
		animFromLeft.setDuration(ANIM_DURATION);
		animFromLeft.setInterpolator(interpolator);
		animFromRight.setDuration(ANIM_DURATION);
		animFromRight.setInterpolator(interpolator);

		numberOfEnemiesText.setAnimation(animFromLeft);
		numberOfEnemies.setAnimation(animFromLeft);
		numberOfEnemiesBar.setAnimation(animFromLeft);

		bouncingText.setAnimation(animFromRight);
		bouncingToggle.setAnimation(animFromRight);

		particlesText.setAnimation(animFromRight);
		particlesToggle.setAnimation(animFromRight);

		sizeOfEnemiesText.setAnimation(animFromLeft);
		enemySizeImage.setAnimation(animFromLeft);
		enemySizeBar.setAnimation(animFromLeft);

		backCustomButton.setAnimation(animFromLeft);
		playCustomButton.setAnimation(animFromLeft);
	}

	private void setOutAnimations()
	{
		final Animation animToLeft = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE,
				-Global.app.consts.width, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
		final Animation animToRight = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE,
				2 * Global.app.consts.width, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);

		// Interpolator interpolator = new DecelerateInterpolator(1.5f);
		final Interpolator interpolator = new LinearInterpolator();
		animToLeft.setDuration(ANIM_OUT_DURATION);
		animToLeft.setInterpolator(interpolator);
		animToRight.setDuration(ANIM_OUT_DURATION);
		animToRight.setInterpolator(interpolator);

		numberOfEnemiesText.setAnimation(animToLeft);
		numberOfEnemies.setAnimation(animToLeft);
		numberOfEnemiesBar.setAnimation(animToLeft);

		bouncingText.setAnimation(animToRight);
		bouncingToggle.setAnimation(animToRight);

		particlesText.setAnimation(animToRight);
		particlesToggle.setAnimation(animToRight);

		sizeOfEnemiesText.setAnimation(animToLeft);
		enemySizeImage.setAnimation(animToLeft);
		enemySizeBar.setAnimation(animToLeft);

		backCustomButton.setAnimation(animToLeft);
		playCustomButton.setAnimation(animToLeft);
	}

	private void setSettingsValues()
	{
		numberOfEnemies.setText(CustomGameSettings.numberOfEnemies + "");
		numberOfEnemiesBar.setMax(MAX_CUSTOM_ENEMIES - MIN_CUSTOM_ENEMIES);
		numberOfEnemiesBar.setProgress(CustomGameSettings.numberOfEnemies - MIN_CUSTOM_ENEMIES);
		bouncingToggle.setChecked(CustomGameSettings.bouncing);
		particlesToggle.setChecked(CustomGameSettings.particles);
		enemySizeBar.setMax(MAX_ENEMY_SIZE);
		enemySizeBar.setProgress(CustomGameSettings.enemiesSize);
		setEnemyThumbnail(CustomGameSettings.enemiesSize);
	}

	private void setViewsVisibility(int visibility)
	{
		numberOfEnemiesText.setVisibility(visibility);
		numberOfEnemies.setVisibility(visibility);
		numberOfEnemiesBar.setVisibility(visibility);

		bouncingText.setVisibility(visibility);
		bouncingToggle.setVisibility(visibility);

		particlesText.setVisibility(visibility);
		particlesToggle.setVisibility(visibility);

		sizeOfEnemiesText.setVisibility(visibility);
		enemySizeImage.setVisibility(visibility);
		enemySizeBar.setVisibility(visibility);

		backCustomButton.setVisibility(visibility);
		playCustomButton.setVisibility(visibility);
	}
}
